﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
using OutlookRss.ServiceRss;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;

namespace OutlookRss
{
    partial class FormRegion1
    {
        List<Flux> listFlux = null;
        LoginModel loginModel = new LoginModel();
        public string _password;
        #region Form Region Factory

        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
        [Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.PostRss)]
        [Microsoft.Office.Tools.Outlook.FormRegionName("OutlookRss.FormRegion1")]
        public partial class FormRegion1Factory
        {
            // Occurs before the form region is initialized.
            // To prevent the form region from appearing, set e.Cancel to true.
            // Use e.OutlookItem to get a reference to the current Outlook item.
            private void FormRegion1Factory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
            {
            }
        }

        #endregion

        // Occurs before the form region is displayed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
        {

        }

        // Occurs when the form region is closed.
        // Use this.OutlookItem to get a reference to the current Outlook item.
        // Use this.OutlookFormRegion to get a reference to the form region.
        private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
        {
        }

        private void loadFlux()
        {
            try
            {
                using (ServiceRssClient client = new ServiceRssClient())
                {
                    this.listFlux = client.GetFlux(loginModel).ToList();
                    listBox1.DataSource = listFlux.ToArray();
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Serveur Exeption :" + e.Message);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            using (ServiceRssClient client = new ServiceRssClient())
            {
                loginModel.Password = this.password.Text;
                loginModel.UserName = this.login.Text;

                try
                {
                    MembershipCreateStatus status = client.LogOn(loginModel);
                    if (status == MembershipCreateStatus.Success)
                    {
                        this.label3.Text = "Connected : " + status.ToString();
                        this.button3.Enabled = true;
                        loadFlux();
                    }
                    else
                    {
                        string text = "Error : ";
                        this.label3.Text = text + status.ToString();
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Serveur Exeption :" + ex.Message);
                }
            }
        }



        private void getRss(Flux flux)
        {
            using (ServiceRssClient client = new ServiceRssClient())
            {
                var rss = client.GetRss(loginModel, flux);

                string content = "";
                string sep = "<br />";

                foreach (Rss article in rss)
                {
                    content += "<h1>" + article.Title + "</h1>";
                    content += article.Description + sep;
                    content += "<a href='" + article.Link + "'>Read More</a><br /><br /><br />";
                }
                webBrowser1.DocumentText = content;
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Flux selectedFlux = listBox1.SelectedItem as Flux;

            if (selectedFlux != null)
            {
                getRss(selectedFlux);
            }
        }


        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                using (ServiceRssClient client = new ServiceRssClient())
                {
                    Flux selectedFlux = listBox1.SelectedItem as Flux;
                    DeleteRss delteRss = new DeleteRss { IdFlux = selectedFlux.ID, UserName = loginModel.UserName };
                    FluxStatus success = client.RemoveRss(this.loginModel, delteRss);
                    if (success == FluxStatus.Success)
                    {
                        this.loadFlux();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Serveur Exeption :" + ex.Message);
            }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Flux selectedFlux = listBox1.SelectedItem as Flux;
            this.TitleEdit.Text = selectedFlux.Title;
            this.UrlEdit.Text = selectedFlux.Link;
            this.label10.Text = selectedFlux.ID.ToString();
            this.button2.Enabled = true;
            }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                using (ServiceRssClient client = new ServiceRssClient())
                {
                    Flux selectedFlux = listBox1.SelectedItem as Flux;

                    EditFlux editFlux = new EditFlux { ID = int.Parse(this.label10.Text), Title = this.TitleEdit.Text, Link = this.UrlEdit.Text };

                    FluxStatus success = client.EditRss(this.loginModel, editFlux);
                    if (success == FluxStatus.Success)
                    {
                        this.button2.Enabled = false;
                        selectedFlux.Link = this.UrlEdit.Text;
                        selectedFlux.Title = this.TitleEdit.Text;
                        this.UrlEdit.Text = "";
                        this.TitleEdit.Text = "";
                    }

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Serveur Exeption :" + ex.Message);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                using (ServiceRssClient client = new ServiceRssClient())
                {
                    RegisterRss registerRss = new RegisterRss { Link = this.UrlAdd.Text, Title = this.TitleAdd.Text, UserName = loginModel.UserName };

                    FluxStatus success = client.AddRss(this.loginModel, registerRss);
                    if (success == FluxStatus.Success)
                    {
                        this.UrlAdd.Text = "";
                        this.TitleAdd.Text = "";
                        this.loadFlux();

                    }
                }
            }
            catch (Exception ex)
            {
                 MessageBox.Show("Serveur Exeption :" + ex.Message);
            }
        }

    }
}
